pandas之表格条件格式设置

您所在的位置:网站首页 pandas 列名修改颜色 pandas之表格条件格式设置

pandas之表格条件格式设置

2024-03-28 14:26| 来源: 网络整理| 查看: 265

获取想要设置格式的数据

import pandas as pd df=pd.read_csv("C:\\Users\\Desktop\\xz.csv",encoding='GBK',header=None) df1=df.loc[:,1:5] df1.columns=['xc','xr','xx','cx','sq'] import numpy as np df1.loc[4:6,['xc']]=np.nan

在这里插入图片描述 1、变量大小用横向条形图展示

#呈现条形图 df1.style.bar("xx",vmin=0)

在这里插入图片描述 2、负值,字体变红色

def negative_color_red(val): color='red' if val < 0 else 'black' return 'color: %s'% color df1.style.applymap(negative_color_red) ##显示负数

在这里插入图片描述 3、每列最大值的背景色变黄色

def highlight_max(s): ''' highlight the maximum in a Series yellow. ''' is_max = s == s.max() return ['background-color: yellow' if v else '' for v in is_max] df1.style.apply(highlight_max)

在这里插入图片描述

4、空值背景色为红色

df1.style.highlight_null() #df1.style.highlight_null(null_color='green') ##背景色变绿色

在这里插入图片描述

5、指定列的数值为负值,字体变红色

df1.style.applymap(color_negative_red, subset=pd.IndexSlice[2:5, ['cx', 'sq']])

在这里插入图片描述

6、背景色呈现阶梯变化

#背景颜色呈现阶梯变化 df1.style.background_gradient("Greens",subset="xr")

在这里插入图片描述 在这里插入图片描述

7、条形图、阶梯和空值设置 在这里插入图片描述

8、数据添加百分比 在这里插入图片描述

9、数据小于4位填充0,加个+号及2位数

df1.style.format({'xx': "{:0


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3